using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using OgilvyOne.Model;
using OgilvyOne.IDAL;
using System.Collections;

namespace OgilvyOne.SqlServerDAL
{
    public class MenuFunc : IMenuFunc
    {
        // Fields
        private const string PARM_FUNCTION_UID = "@Function_UID";
        private const string PARM_FUNCTION_UIDS = "@Function_UIDS";
        private const string PARM_ISDEL = "@IsDel";
        private const string PARM_MENU_FUNCTION_UID = "@SMF_UID";
        private const string PARM_MENU_UID = "@Menu_UID";
        private const string PARM_SMF_WRITE = "@SMF_Write";
        private const string SQL_DELETE_MENU_FUNCTION = "UPDATE Sys_Menu_Function SET IsDel = '1' WHERE SMF_UID = @SMF_UID";
        private const string SQL_DELETE_MENULIST_FUNCTION = "UPDATE Sys_Menu_Function SET IsDel = '1' WHERE Function_UID = @Function_UID";
        private const string SQL_INSERT_MENU_FUNCTION = "INSERT INTO Sys_Menu_Function VALUES(@Menu_UID,@Function_UID,@SMF_Write,'0')";
        private const string SQL_SELECT_MENU_FUNCTION = "SELECT SMF_UID,Menu_UID,Function_UID,SMF_Write FROM Sys_Menu_Function  where SMF_UID=@SMF_UID and  IsDel='0'";
        private const string SQL_SELECT_MENU_FUNCTION_LIST = "SELECT SMF_UID,Menu_UID,Function_UID,SMF_Write FROM Sys_Menu_Function where IsDel='0'";
        private const string SQL_SELECT_MENU_FUNCTION_LIST_FUID = "SELECT SMF_UID,Menu_UID,Function_UID,SMF_Write FROM Sys_Menu_Function where IsDel='0' and Function_UID=@Function_UID";
        private const string SQL_SELECT_MENU_FUNCTION_LIST_FUIDS = "SELECT SMF_UID,Menu_UID,Function_UID,SMF_Write FROM Sys_Menu_Function where IsDel='0' and Function_UID in ({0})";
        private const string SQL_UPDATE_MENU_FUNCTION = "UPDATE Sys_Menu_Function SET Menu_UID = @Menu_UID,SMF_Write=@SMF_Write Function_UID = @Function_UID WHERE SMF_UID = @SMF_UID";

        // Methods
        public void Delete(int menufunc_id)
        {
            SqlParameter[] menuFunctionDeleteParameters = GetMenuFunctionDeleteParameters();
            menuFunctionDeleteParameters[0].Value = menufunc_id;
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, "UPDATE Sys_Menu_Function SET IsDel = '1' WHERE SMF_UID = @SMF_UID", menuFunctionDeleteParameters);
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }

        public void DeleteMenuList(int func_id)
        {
            SqlParameter[] menuListDeleteParameters = GetMenuListDeleteParameters();
            menuListDeleteParameters[0].Value = func_id;
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, "UPDATE Sys_Menu_Function SET IsDel = '1' WHERE Function_UID = @Function_UID", menuListDeleteParameters);
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }

        public string getMenuByFuncUID(string funcid)
        {
            string str = "0";
            string cmdText = string.Format("SELECT SMF_UID,Menu_UID,Function_UID,SMF_Write FROM Sys_Menu_Function where IsDel='0' and Function_UID in ({0})", funcid);
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, cmdText, null))
            {
                while (reader.Read())
                {
                    str = str + "," + reader.GetInt32(1);
                }
            }
            return str;
        }

        public IList getMenuFuncList()
        {
            IList list = new ArrayList();
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, "SELECT SMF_UID,Menu_UID,Function_UID,SMF_Write FROM Sys_Menu_Function where IsDel='0'", null))
            {
                while (reader.Read())
                {
                    MenuFuncInfo info = new MenuFuncInfo(reader.GetInt32(0), reader.GetInt32(1), reader.GetInt32(2), reader.GetString(3));
                    list.Add(info);
                }
            }
            return list;
        }

        public IList getMenuFuncList(int menufuncid)
        {
            IList list = new ArrayList();
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SMF_UID", SqlDbType.Int, 4, "SMF_UID") };
            cmdParms[0].Value = menufuncid;
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, "SELECT SMF_UID,Menu_UID,Function_UID,SMF_Write FROM Sys_Menu_Function  where SMF_UID=@SMF_UID and  IsDel='0'", cmdParms))
            {
                while (reader.Read())
                {
                    MenuFuncInfo info = new MenuFuncInfo(reader.GetInt32(0), reader.GetInt32(1), reader.GetInt32(2), reader.GetString(3));
                    list.Add(info);
                }
            }
            return list;
        }

        private static SqlParameter[] GetMenuFunctionDeleteParameters()
        {
            SqlParameter[] cachedParameters = SQLHelper.GetCachedParameters("UPDATE Sys_Menu_Function SET IsDel = '1' WHERE SMF_UID = @SMF_UID");
            if (cachedParameters == null)
            {
                cachedParameters = new SqlParameter[] { new SqlParameter("@SMF_UID", SqlDbType.Int, 4) };
                SQLHelper.CacheParameters("UPDATE Sys_Menu_Function SET IsDel = '1' WHERE SMF_UID = @SMF_UID", cachedParameters);
            }
            return cachedParameters;
        }

        private static SqlParameter[] GetMenuFunctionInsertParameters()
        {
            SqlParameter[] cachedParameters = SQLHelper.GetCachedParameters("INSERT INTO Sys_Menu_Function VALUES(@Menu_UID,@Function_UID,@SMF_Write,'0')");
            if (cachedParameters == null)
            {
                cachedParameters = new SqlParameter[] { new SqlParameter("@Menu_UID", SqlDbType.Int, 4), new SqlParameter("@Function_UID", SqlDbType.Int, 4), new SqlParameter("@SMF_Write", SqlDbType.Char, 4) };
                SQLHelper.CacheParameters("INSERT INTO Sys_Menu_Function VALUES(@Menu_UID,@Function_UID,@SMF_Write,'0')", cachedParameters);
            }
            return cachedParameters;
        }

        private static SqlParameter[] GetMenuFunctionModifyParameters()
        {
            SqlParameter[] cachedParameters = SQLHelper.GetCachedParameters("UPDATE Sys_Menu_Function SET Menu_UID = @Menu_UID,SMF_Write=@SMF_Write Function_UID = @Function_UID WHERE SMF_UID = @SMF_UID");
            if (cachedParameters == null)
            {
                cachedParameters = new SqlParameter[] { new SqlParameter("@SMF_UID", SqlDbType.Int, 4), new SqlParameter("@Menu_UID", SqlDbType.Int, 4), new SqlParameter("@Function_UID", SqlDbType.Int, 4), new SqlParameter("@SMF_Write", SqlDbType.Char, 4) };
                SQLHelper.CacheParameters("UPDATE Sys_Menu_Function SET Menu_UID = @Menu_UID,SMF_Write=@SMF_Write Function_UID = @Function_UID WHERE SMF_UID = @SMF_UID", cachedParameters);
            }
            return cachedParameters;
        }

        public IList getMenuFuncUIDList(int funcid)
        {
            IList list = new ArrayList();
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Function_UID", SqlDbType.Int, 4, "Function_UID") };
            cmdParms[0].Value = funcid;
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, "SELECT SMF_UID,Menu_UID,Function_UID,SMF_Write FROM Sys_Menu_Function where IsDel='0' and Function_UID=@Function_UID", cmdParms))
            {
                while (reader.Read())
                {
                    MenuFuncInfo info = new MenuFuncInfo(reader.GetInt32(0), reader.GetInt32(1), reader.GetInt32(2), reader.GetString(3));
                    list.Add(info);
                }
            }
            return list;
        }

        private static SqlParameter[] GetMenuListDeleteParameters()
        {
            SqlParameter[] cachedParameters = SQLHelper.GetCachedParameters("UPDATE Sys_Menu_Function SET IsDel = '1' WHERE Function_UID = @Function_UID");
            if (cachedParameters == null)
            {
                cachedParameters = new SqlParameter[] { new SqlParameter("@Function_UID", SqlDbType.Int, 4) };
                SQLHelper.CacheParameters("UPDATE Sys_Menu_Function SET IsDel = '1' WHERE Function_UID = @Function_UID", cachedParameters);
            }
            return cachedParameters;
        }

        public void Insert(MenuFuncInfo acc)
        {
            SqlParameter[] menuFunctionInsertParameters = GetMenuFunctionInsertParameters();
            this.SetMenuFunctionInsertParameters(menuFunctionInsertParameters, acc);
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, "INSERT INTO Sys_Menu_Function VALUES(@Menu_UID,@Function_UID,@SMF_Write,'0')", menuFunctionInsertParameters);
                        transaction.Commit();
                    }
                    catch (SqlException exception)
                    {
                        transaction.Rollback();
                        Console.WriteLine(exception.Message);
                    }
                }
            }
        }

        private void SetMenuFunctionInsertParameters(SqlParameter[] parms, MenuFuncInfo acc)
        {
            parms[0].Value = acc.Menu_UID;
            parms[1].Value = acc.Function_UID;
            parms[2].Value = acc.SMF_Write;
        }

        private void SetMenuFunctionModifyParameters(SqlParameter[] parms, MenuFuncInfo acc)
        {
            parms[0].Value = acc.SMF_UID;
            parms[1].Value = acc.Menu_UID;
            parms[2].Value = acc.Function_UID;
            parms[3].Value = acc.SMF_Write;
        }

        public void Update(MenuFuncInfo myItem)
        {
            SqlParameter[] menuFunctionModifyParameters = GetMenuFunctionModifyParameters();
            this.SetMenuFunctionModifyParameters(menuFunctionModifyParameters, myItem);
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, "UPDATE Sys_Menu_Function SET Menu_UID = @Menu_UID,SMF_Write=@SMF_Write Function_UID = @Function_UID WHERE SMF_UID = @SMF_UID", menuFunctionModifyParameters);
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
    }
}
